<template>
  <el-dialog :title="$t('message.add')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" :placeholder="$t('message.name')" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{$t('message.search')}}</el-button>
        <el-button @click="clearQueryData()">{{$t('message.clean')}}</el-button>
        <el-button @click="saveOrderInfo()">{{$t('message.submit')}}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        :label="$t('message.name')">
      </el-table-column>
      <!-- <el-table-column
        prop="comment"
        header-align="center"
        align="center"
        :label="$t('message.recommend')">
      </el-table-column> -->
      <el-table-column
        prop="price"
        header-align="center"
        align="center"
        :label="$t('message.price')">
      </el-table-column>
      <el-table-column
        prop="isDiscount"
        header-align="center"
        align="center"
        :label="$t('message.isDiscount')">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isDiscount === 1" size="small">{{$t('message.yes')}}</el-tag>
          <el-tag v-else-if="scope.row.isDiscount === 0" size="small">{{$t('message.no')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="discountPrice"
        header-align="center"
        align="center"
        :label="$t('message.discountPrice')">
      </el-table-column>
      <el-table-column
        prop="kind"
        header-align="center"
        align="center"
        :label="$t('message.goodsKind')">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.kind === 1" size="small">{{$t('message.shoujimo')}}</el-tag>
          <el-tag v-else-if="scope.row.kind === 2" size="small">{{$t('message.jiqi')}}</el-tag>
          <el-tag v-else-if="scope.row.kind === 3" size="small">{{$t('message.lingjian')}}</el-tag>
          <el-tag v-else-if="scope.row.kind === 4" size="small">{{$t('message.cishuma')}}</el-tag>
          <el-tag v-else size="small">{{$t('message.other')}}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="status"
        header-align="center"
        align="center"
        :label="$t('message.status')">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" size="small">{{$t('message.putaway')}}</el-tag>
          <el-tag v-else-if="scope.row.status === 0" size="small" type="danger">{{$t('message.soldOut')}}</el-tag>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        header-align="center"
        align="right"
        :label="$t('message.buyAmount')">
        <template slot-scope="scope">
          <el-input></el-input>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        :label="$t('message.operation')">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addToOrder(scope.row.id)">{{$t('message.addTo')}}</el-button>
        </template>
      </el-table-column> -->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        :label="$t('message.operation')">
        <template slot-scope="scope">
          <!-- <el-button type="text" size="small" @click="addToOrder(scope.row.id)">{{$t('message.addTo')}}</el-button> -->
          <el-button type="text" size="small" @click="addOrderHandle(scope.row)">{{$t('message.addTo')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>

  <el-dialog
    width="30%"
    :title="$t('message.add')"
    :visible.sync="innerVisible"
    append-to-body>
    <el-form :model="selectGoods" ref="selectGoods" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item :label="$t('message.name')" :class="{ 'is-required': true }" >
        <el-input v-model="selectGoods.name" :placeholder="$t('message.name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.amount')" :class="{ 'is-required': true }">
        <el-input-number v-model="selectAmount" :min="1" :max="1000"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="addToOrder(selectGoods.id, selectAmount)">{{$t('message.submit')}}</el-button>
    </span>
  </el-dialog>

  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        innerVisible: false,
        selectGoods: '',
        selectAmount: 1,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        paramGoodsList: [],
        dataForm: {
          orderNo: '',
          company: '',
          dealerName: '',
          dealerEmail: '',
          otherContact: '',
          bladeNo: '',
          bladeExplain: '',
          filmNo: '',
          filmExplain: '',
          deviceNo: '',
          deviceExplain: '',
          useTimes: '',
          comment: '',
          createTime: '',
          status: ''
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/goods/queryGoodsByPage4Sys'),
          method: 'POST',
          data: this.$http.adornData({
            'currPage': this.pageIndex,
            'limit': this.pageSize
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/goods/queryGoodsByPage4Sys'),
          method: 'POST',
          data: this.$http.adornData({
            'currPage': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.kindList = data.kindList
            this.totalPage = data.page.totalCount
            this.isAdmin = data.isAdmin
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      saveOrderInfo () {
        if (this.paramGoodsList.length === 0) {
          this.$message.error(this.$t('message.choseGoods'))
        } else {
          this.$http({
            url: this.$http.adornUrl(`/sys/sysOrder/createOrder`),
            method: 'post',
            data: this.$http.adornData({
              'paramGoodsList': this.paramGoodsList
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: this.$t('message.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      },
      handleAvatarSuccess: function (response) {
        this.goodsImageList.push(response.imagePath)
      },
      beforeAvatarUpload (file) {
        var test1 = /^image\/(jpeg|png|jpg)$/.test(file.type)
        const isLt5M = file.size / 1024 / 1024 <= 5
        var image5 = this.goodsImageList.length <= 3
        if (!image5) {
          this.$message.error(this.$t('message.imgLimit'))
        }
        if (!test1) {
          this.$message.error(this.$t('message.caption'))
        }
        if (!isLt5M) {
          this.$message.error(this.$t('message.uploadTip'))
        }
        return test1 && isLt5M && image5
      },
      handleRemove (file, fileList) {
        this.goodsImageList.pop(file)
      },
      handlePictureCardPreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      clearQueryData () {
        this.dataForm.name = ''
        this.getDataList()
      },
      addToOrder (goodsId, amount) {
        for (var i = 0; i < this.paramGoodsList.length; i++) {
          if (this.paramGoodsList[i].goodsId === goodsId) {
            this.paramGoodsList.pop(this.paramGoodsList[i])
          }
        }
        var item = {}
        item.goodsId = goodsId
        item.amount = amount
        this.paramGoodsList.push(item)

        this.innerVisible = false
        this.selectAmount = 0
      },
      addOrderHandle (goods) {
        this.innerVisible = true
        this.selectGoods = goods
        this.selectAmount = 0
      }
    }
  }
</script>
